<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章信息展示</title>
    <style>
        body {
            margin: 0;
        }

        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        a{
            text-decoration: none;
            color: #404040;
        }
        .wrap{
            width: 600px;
            margin: 0 auto;
        }

        .news-list {
            width: 600px;
        }

        .news {
            width: 100%;
            display: flex;
            justify-content: space-between;
            padding: 15px 0;
            border-bottom: 1px solid #999;
        }

        .info {
            display: flex;
            width: 200px;
            justify-content: space-between;
            font-size: 12px;
            color: #888;
        }

        .tips {
            display: flex;
            width: 60px;
            justify-content: space-between;
        }
        .news-list{
            min-height: 686px;
        }
        .news-list li:nth-child(5) {
            border-bottom: none;
        }
        .pagination{
            display: flex;
            width: 300px;
            text-align: center;
            background-color: rgb(252, 238, 238);
            border-radius: 25px;
            overflow: hidden;
            margin: 0 auto;
        }
        .pagination a{
            width: 30px;
            line-height: 30px;
            color: #404040;
            flex:1;
        }
        .pagination a:nth-child(1) {
            transform: rotate(-45deg) ;
        }
        .pagination .next {
            transform: rotate(45deg) ;
        }
        .pagination a:hover{
            color: rgb(247, 73, 73);
        }
        .newsContainer{
            width: 400px;
        }
        .news img{
            width: 140px;
            height: 88px;
        }
        /* .active{
            color: rgb(247, 73, 73);
        } */
    </style>
    <script src="./ajax.js"></script>
</head>

<body>
    <div class="wrap">
        <ul class="news-list">
        </ul>
        <div class="pagination">
            <!-- <a href="javascript:;" class="prev">⌜</a>
            <a href="javascript:;">1</a>
            <a href="javascript:;">2</a>
            <a href="javascript:;">3</a>
            <a href="javascript:;">4</a>
            <a href="javascript:;">5</a>
            <a href="javascript:;" class="next">⌝</a> -->
        </div>
    </div>
    <script>
        let page = 1 ; 
        let time = (new Date()).getTime();
        // console.log(time);
        getList();
        function getList(){
            ajax({
                url:"./allNews.php",
                method:"post",
                dataType:"json",
                data:{
                    page
                },
                success(data){
                    console.log(data);
                    renderDom(data);
                    getPage();
                }
            });
        }
        
        function renderDom(data){
            let ulEle = document.querySelector(".news-list");
            ulEle.innerHTML = '' ;
            data.forEach(item => {
                let liEle = document.createElement("li");
                liEle.className = "news";
                liEle.innerHTML = 
                `<a href="javascript:;" class="btn">
                    <img src="./uploads/${item['imgSrc']}" alt="">
                </a>
                <div class="newsContainer">
                    <h3>
                        <a href="javascript:;" class="title">${item['header']}</a>
                    </h3>
                    <div class="info">
                        <span class="tips"><span>${item['countryname']}</span><span>${item['typename']}</span></span>
                        <!-- <span class="line"></span> -->
                        <span class="time">| &nbsp;&nbsp;${parseInt((time / 1000 - item['ptime'])/3600/24)}天${parseInt((time / 1000 - item['ptime'])/3600)%24}小时${parseInt((time / 1000 - item['ptime'])/60%60)}分钟前</span>
                    </div>
                </div>`;
                let a1Ele = liEle.querySelector(".btn");
                let a2Ele = liEle.querySelector(".title");
                a1Ele.onclick = function(){
                    window.location.href=`detail.php?id=${item['newId']}`;
                }
                a2Ele.onclick = function(){
                    window.location.href=`detail.php?id=${item['newId']}`;
                }
                ulEle.appendChild(liEle);
            });
        }
        getPage();
        function getPage(){
            ajax({
                url:"getPage.php",
                method:"post",
                dataType:"json",
                success(data){
                    let page = data['totalPage'];
                    pageRender(page);
                }
            });
        }
        
        function pageRender(num){
            let pagEle = document.querySelector(".pagination");
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= num ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    // aEles.forEach(function(item,index){
                    //     if( key == index ){
                    //         aEle.classList.add("active");
                    //     } else {
                    //         aEle.classList.remove("active");
                    //     }
                    // });
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                            getList();
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < num ){
                            page = page + 1 ;
                            getList();
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                        getList();
                    }
                }
                
            });
        }
    </script>
</body>
</html>